// pages/recruitStatus/recruitStatus.js
var app = getApp()
var time= require("../../utils/util.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    recruit:null,  //获取当前按压的对象
    recruitAll:[], //用户所有的招募
    // recruit:[],  //根据id加载不同的招募列表
    winHeight:"",//窗口高度
    currentTab:0, //预设当前项的值
    scrollLeft:0, //tab标题的滚动条位置
    isShow:[] ,//是否显示出来
    timeState:[], //判断活动是否正在进行
    startTime:[],//开始时间,将时间戳转化为标准日期 //时间戳转为日期   let dataTime = time.formatTimeTwo(timeTamp,'Y/M/D')
    zzc:'none',  //遮罩层
    more:'none', //更多功能栏
    more_id:'',//长按时获取到的招募id
    resumeFileIds:[] , //删除招募时需要删除的申请表的简历
  },

   //判断当前滚动超过一屏时，设置tab标题滚动条。
   checkCor:function(){
    if (this.data.currentTab>4){
      this.setData({
        scrollLeft:300
      })
    }else{
      this.setData({
        scrollLeft:0
      })
    }
  },
  // 判断招募是否正在进行
  isRecruitActivity:function(){
    const recruitAll =this.data.recruitAll
    let  timeState=[] 
    let startTime=[]
    for (let index = 0; index < recruitAll.length; index++) {
     let dataTime = time.formatTimeTwo(recruitAll[index].startTime,'Y.M.D')
     startTime.push(dataTime)
     if( Date.parse(new Date())/1000-recruitAll[index].endTime>0) {
      //  已结束
      timeState.push('已结束')
     }else{
      timeState.push('进行中')
     }
    }
    this.setData({
      startTime:startTime,
      timeState:timeState
    })
  },  
  toDetailsTap:function(e){
    console.log(e)
    const id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/details/details?id='+id,
    })
  },
  // 申请列表
  applicationsList:function(e){
    console.log(e)
    const id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/applicationsList/applicationsList?id='+id,
    })
  },

   // 长按触发删除，修改视图
   handleLongPress:function(e){
    console.log(e)
    const id = e.currentTarget.dataset.id  //传递id

    const recruit = e.currentTarget.dataset.recruit //传递对象 用于删除静态资源

    console.log('触发了长按事件')
    this.setData({
      zzc:'block',
      more:'block',
      more_id:id,
      recruit:recruit,
    })
  },
  // 点击遮罩层隐藏
  zzc:function(){
    this.setData({
      zzc:'none',
      more:'none',
    })
  },
  removeApplicantByRecruitId:function(recruitId){  //根据招募id删除申请表数据
    wx.cloud.callFunction({
      name:'applicant',
      data:{
        parameter:'removeApplicantByRecruitId',
        recruitId:recruitId,
      }
    }).then(res=>{
      console.log('删除申请表成功',res)
    }).catch(err=>{
      console.log('删除申请表失败',err)
    })
  },
  //根据招募id查询申请表的信息 用来获取简历fileID
  getResumeFileIds:function(recruitId){
    let that = this
    let resumeFileIds = []
    return new Promise((reslove, reject) => {
      wx.cloud.callFunction({
        name:'applicant',
        data:{
          parameter:'getApplicantByRecruitId',
          recruitId:recruitId,
        },
        success: (res) => {
          reslove(res.result);
          console.log('查询申请表成功',res)
          for (let index = 0; index < res.result.length; index++) {
            resumeFileIds.push(res.result[index].enclosure)
          }
          that.setData({
            resumeFileIds:resumeFileIds
          })
          console.log('获取简历列表成功',that.data.resumeFileIds)
        },
        fail: (msg) => {
          reject('请求失败');
        }
      })
    })
  },
  // 删除申请表里面的申请简历、招募表里面的附件和上传的图片
  deleteFile:function(fileList){  //fileList是云文件的id数组
    wx.cloud.deleteFile({
      fileList: fileList
    }).then(res => {
      // handle success
      console.log('成功删除以下静态资源',res.fileList)
    }).catch(error => {
      // handle error
    })
  },

  //根据id进行删除
  deleteRecruit:function(){
    const id =  this.data.more_id
    console.log(id)
    let that = this
    wx.showModal({
      title: '提示',
      content: '删除后不可撤销',
      success: function (res) {
        if (res.confirm) {//这里是点击了确定以后
          wx.showLoading({
            title: '删除中',
          })
          wx.cloud.callFunction({
            name:'recruit',
            data:{
              parameter:'removeRecruitById',
              id:id
            }
          }).then(res=>{
            console.log('删除成功',res)
            // 删除招募的imgs 如果是默认图片，不删除
            if(!(that.data.recruit.imgs[0]=='cloud://cloud1-2guh9tam1ee71336.636c-cloud1-2guh9tam1ee71336-1305678983/imgs/custom/custom1.jpg')
            &&!(that.data.recruit.imgs[0]=='cloud://cloud1-2guh9tam1ee71336.636c-cloud1-2guh9tam1ee71336-1305678983/imgs/custom/custom2.jpg')
            &&!(that.data.recruit.imgs[0]=='cloud://cloud1-2guh9tam1ee71336.636c-cloud1-2guh9tam1ee71336-1305678983/imgs/custom/custom3.jpg')){
              that.deleteFile(that.data.recruit.imgs)
            }
            // 删除附件
            let enclosureUrl = []
            enclosureUrl.push(that.data.recruit.enclosureUrl)
            that.deleteFile(enclosureUrl)
            that.getResumeFileIds(id).then(res=>{
              // 根据招募id 级联删除申请表信息 
              that.removeApplicantByRecruitId(id)
               // 删除申请表的简历附件
              that.deleteFile(that.data.resumeFileIds)
            })
            wx.hideLoading()
            that.onRefresh() //刷新页面
          }).catch(err=>{
            console.log('删除失败',err)
          })
          that.zzc()
        } else {//这里是点击了取消以后
          console.log('用户点击取消')
        }
      }
    })
  },
  //根据id进行修改
  updateRecruit:function(){
    const id =  this.data. more_id
    console.log(id)
    let that = this
    wx.showModal({
      title: '提示',
      content: '修改功能还没有开发哦',
      success: function (res) {
        if (res.confirm) {//这里是点击了确定以后
          
          // that.zzc()
        } else {//这里是点击了取消以后
          console.log('用户点击取消')
        }
      }
    })
  },
  // 加载页面数据
  recruitStatus:function(id){
     //申请状态默认为0，0审核中，通过1，不通过2
     const recruitAlls = wx.getStorageSync('recruitAll')
     let recruitAll=[]
     for (let index = 0; index < recruitAlls.length; index++) {
       if(recruitAlls[index].recruitState==id){  // 0审核中
         recruitAll.push(recruitAlls[index]) 
       }
     }
     this.setData({
      recruitAll:recruitAll
     })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options.id)
    if(options.id=='0'){
      wx.setNavigationBarTitle({
        title: '审核中'
      })
      this.recruitStatus(0)
    }else if(options.id=='2'){
      wx.setNavigationBarTitle({
        title: '已通过'
      })
      this.recruitStatus(1)
    }else if(options.id=='3'){
      wx.setNavigationBarTitle({
        title: '未通过'
      })
      this.recruitStatus(2)
    }else{
      
    }
    var that = this; 
    //  高度自适应
    wx.getSystemInfo( {  
        success: function( res ) {  
            var clientHeight=res.windowHeight,
                clientWidth=res.windowWidth,
                rpxR=750/clientWidth;
          var  calc=clientHeight*rpxR-180;
            console.log(calc)
            that.setData( {  
                winHeight: calc  
            });  
        }  
    });
    this.isRecruitActivity()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  onRefresh(){
    wx.showLoading({
      title: '刷新中...',
    })
    this.getData();
    //隐藏loading 提示框
  },
  // 下拉刷新  获取数据
  getData(){
    
    app.getRecruitAll().then(res=>{
      this.setData({
        recruitAll:wx.getStorageSync('recruitAll')
      })
      this.isRecruitActivity()
      wx.hideLoading();
    }).catch(err=>{
      console.log('下拉刷新失败',err)
      wx.hideLoading();
    })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})